<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Logging
 * @version $Id$
 */

/**	
 * A Pattern Layout Formater
 * 
 * This layout formater creates messages according to the given format string. Formating 
 * options of PHP function date() might be used in this string. Moreover, following additional
 * options has been added:
 * - %I log level expressed as one character 
 *   - I Information
 *   - W Warning
 *   - E Error
 *   - F Fatal 
 *   - T Trace
 *   - D Debug
 * - %X log level as string (see above)
 * - %M log message 
 * - %F php file where this log has been created
 * - %L coding line where this log has been created
 * - %C function or class/method call where this og has been created
 * - %e elapsed time between to log entries
 * <code>
 * 	Example:
 * 		Pattern d/m/Y H:i [%X]: - %F, (%L): %M creates a message like:
 * 		31/12/2008 12:35 [Error] - test.php, (123): my message
 * </code>
 *	
 * @package auxo
 * @subpackage Logging
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class  tx_auxo_LogPatternLayout implements tx_auxo_LogLayoutInterface {
	/**
	 * Short labels for all log levels
	 *
	 * @var array
	 */
	private $shortLabels = array( 
								tx_auxo_Logger::DEBUG => 'D',
								tx_auxo_Logger::FATAL => 'F',
								tx_auxo_Logger::ERROR => 'E',
								tx_auxo_Logger::WARNING => 'W',
								tx_auxo_Logger::TRACE => 'T',
								tx_auxo_Logger::INFO => 'I'
						 	);

	/**
	 * Labels for all log levels
	 *
	 * @var array
	 */
	private $longLabels = array( 
								tx_auxo_Logger::DEBUG => 'Debug',
								tx_auxo_Logger::FATAL => 'Fatal',
								tx_auxo_Logger::ERROR => 'Error',
								tx_auxo_Logger::WARNING => 'Warning',
								tx_auxo_Logger::TRACE => 'Trace',
								tx_auxo_Logger::INFO => 'Info'
						 	);
	
	/**
	 * last time this formater has been used
	 *
	 * @var int
	 */					
	private $lastTimestamp = 0;
	
	
	/**
	 * list of allowed pattern codes
	 * 
	 * @var array
	 */
	private $patternCodes = array ( '%X', '%I', '%M', '%e', '%F', '%L', '%C' );
	
	
	/**
	 * currently defined pattern
	 * 
	 * @var string
	 */
	private $pattern;
	
	/**
	 * Creates a new pattern formater
	 *
	 * @param string $pattern string with pattern codes which are substituted during logging
	 */
	public function __construct($pattern) {
		$this->pattern = $pattern;
	}
	
	/**
	 * Returns Content Type of this appender
	 *
	 * @return string $mimetype mimetype
	 */
	public function getContentType() {
		return ('text/text');
	}
						 	
	/**
	 * Formats an log event and returns a string
	 *
	 * @param tx_auxo_LogEvent $event A log event
	 * @return string $output Log event formated as string
	 */
	public function format(tx_auxo_LogEvent $event) {
		$output = $this->pattern;
		
		foreach ($this->patternCodes as $patternCode) {
			if (strpos($patternCode, $this->pattern)) {
				switch ($patternCode) {
					case '%I':
						$replacement = $this->shortLabels[$event->getLevel()];
						break;
					case '%X':
						$replacement = $this->longLabels[$event->getLevel()];
						break;
					case '%F':
						$replacement = $event->getSourceFilename();
						break;
					case '%L':
						$replacement = $event->getLineNumber();
						break;
					case '%C':
						$replacement = $event->getFunctionCall();
						break;
					case '%M':
						$replacement = $event->getMessage();
						break;
					case '%e':
						$replacement = sprintf('%f3.4', $event->getTimestamp() - $this->lastTimestamp);
						break;
					default:
						$replacement = '';
				}
				
				$output = str_replace($patternCode, $replacement, $output);
			}
		}
		$this->lastTimestamp = $event->getTimestamp();
		$output = date($output, $event->getTimestamp());		
		return $output;
	}
	
	/**
	 * Returns a log header 
	 *
	 * @return string $header returns a header for log entries
	 */
	public function getHeader() {		 
		return '';
	}
	
	/**
	 * Returns a log footer
	 *
	 * @return string $footer returns a footer for log entries
	 */
	public function getFooter() {
		return '';
	}
}
?>